package com.cdd.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdd.commonutils.JwtUtils;
import com.cdd.commonutils.R;
import com.cdd.order.entity.Order;
import com.cdd.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author cdd
 * @since 2021-02-15
 */
@RestController
@RequestMapping("/order/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    //根据课程id和用户id创建订单，返回订单id
    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request){
        String orderNo = orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId",orderNo);
    }

    // 根据id获取订单信息接口
    @GetMapping("getOrder/{orderId}")
    public R getOrder(@PathVariable String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item", order);
    }

}

